﻿Public Partial Class ProductIndex_Bulk_Popup
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData()
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not (Session("Booking_Index") Is Nothing) Then
                Dim dt As DataTable = clsGet.Get_Booking_ProductOperationBulk(Session("Booking_Index"))
                gridProduct.DataSource = dt
                gridProduct.DataBind()
                Dim First As Boolean = True
                For i As Integer = 0 To gridProduct.Rows.Count - 1
                    Dim ddlIndex As DropDownList = CType(gridProduct.Rows(i).FindControl("ddlIndex"), DropDownList)
                    If (First) Then
                        ddlIndex.ClearSelection()
                        ddlIndex.Items.Clear()
                    End If
                    For b As Integer = 1 To gridProduct.Rows.Count
                        ddlIndex.Items.Insert(b - 1, b.ToString())
                        ddlIndex.Items(b - 1).Value = b.ToString()
                    Next
                    ddlIndex.SelectedValue = i + 1
                Next
            End If
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Session("Booking_Index") = Nothing
        Session("Index_For_Save") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.HideIndex('N');", True)
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        For i As Integer = 0 To gridProduct.Rows.Count - 2
            Dim ddlIndex As DropDownList = CType(gridProduct.Rows(i).FindControl("ddlIndex"), DropDownList)
            For n As Integer = (i + 1) To gridProduct.Rows.Count - 1
                Dim ddlIndex2 As DropDownList = CType(gridProduct.Rows(n).FindControl("ddlIndex"), DropDownList)
                If (ddlIndex.SelectedValue = ddlIndex2.SelectedValue) Then
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ลำดับของสินค้าซ้ำ!!');", True)
                    Exit Sub
                End If
            Next
        Next
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Dim BP_Code As String = "''"
        Application.Lock()
        conn.Open()

        tran = clsDB.BeginTran(conn)
        clsFc.UpdateClear_Index(Session("Booking_Index"), conn, tran)
        For i As Integer = 0 To gridProduct.Rows.Count - 1
            Dim ddlIndex As DropDownList = CType(gridProduct.Rows(i).FindControl("ddlIndex"), DropDownList)
            If (clsFc.UpdateBooking_Product_Index(gridProduct.DataKeys(i).Values("Booking_no"), gridProduct.DataKeys(i).Values("Product_no"), ddlIndex.SelectedValue, "", "", "", gridProduct.DataKeys(i).Values("Product_Buyer_name"), gridProduct.DataKeys(i).Values("Product_Seller_name"), BP_Code, "", "", "", gridProduct.DataKeys(i).Values("Owner_Product_no"), conn, tran) < 1) Then
                condition = False
            Else
                BP_Code &= ",'" & gridProduct.DataKeys(i).Values("Booking_no") & gridProduct.DataKeys(i).Values("Product_no") & ddlIndex.SelectedValue & "'"
            End If
        Next

        If (condition) Then
            clsDB.CommitTran(tran)
            Session("Booking_Index") = Nothing
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.HideIndex('S');", True)
        Else
            clsDB.RollBackTran(tran)
        End If
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class